package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.entity.tree.TreeEntity;
import com.atguigu.eduservice.feign.VodFeign;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.ELeanExeception;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-01-06
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 课程大纲列表，根据课程id进行查询
     *
     * @param courseId
     * @return
     */
    @Override
    public List<TreeEntity> getChapterVideoByCourseId(String courseId) {
        return buildTreeData(courseId);
    }

    /**
     * 根据章节id删除章节
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        if (selectVideoByChapterId(chapterId)){
            throw new ELeanExeception(20001,"该章节存在小节,不能删除");
        }else {//删除章节
            return baseMapper.deleteById(chapterId) > 0 ? true:false;
        }
    }

    /**
     * 根据课程id删除章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        baseMapper.delete(new QueryWrapper<EduChapter>().eq("course_id",courseId));
    }

    /**
     * 根据chapterId章节id  查询小节表 ，如果查询有数据，不进行删除返回true
     * @param chapterId
     * @return
     */
    private boolean selectVideoByChapterId(String chapterId){
        QueryWrapper<EduVideo> qw = new QueryWrapper<>();
        qw.eq("chapter_id",chapterId);
        return eduVideoService.count(qw)>0 ? true:false;
    }

    /**
     * 构建前端所需的树结构
     * @param courseId
     * @return
     */
    private List<TreeEntity> buildTreeData(String courseId) {
        //1.根据课程id查询课程里面所有的章节
        List<EduChapter> eduChapters = selectChaptersByCourseId(courseId);
        //2.根据课程id查询课程里面所有的小节
        List<EduVideo> eduVideos = selectVideosByCourseId(courseId);
        List<TreeEntity> returnList = new ArrayList<>();
        if (eduChapters != null && eduChapters.size() > 0) {
            //3.遍历查询章节list集合进行封装
            for (int i = 0; i < eduChapters.size(); i++) {
                EduChapter eduChapter = eduChapters.get(i);
                TreeEntity treeEntity = new TreeEntity();
                BeanUtils.copyProperties(eduChapter, treeEntity);
                if (eduVideos != null && eduVideos.size() > 0) {
                    //创建集合，用于封装章节的小节
                    List<TreeEntity> videoList = new ArrayList<>();
                    //4.遍历查询小节list集合，进行封装
                    for (int m = 0; m < eduVideos.size(); m++) {
                        EduVideo eduVideo = eduVideos.get(m);
                        TreeEntity video = new TreeEntity();
                        BeanUtils.copyProperties(eduVideo, video);
                        if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                            videoList.add(video);
                            treeEntity.setChildren(videoList);
                        }
                    }
                }
                returnList.add(treeEntity);
            }

        }
        return returnList;
    }

    private List<EduChapter> selectChaptersByCourseId(String courseId) {
        QueryWrapper<EduChapter> qw = new QueryWrapper<>();
        qw.eq("course_id", courseId);
        return baseMapper.selectList(qw);
    }

    private List<EduVideo> selectVideosByCourseId(String courseId) {
        QueryWrapper<EduVideo> qw = new QueryWrapper<>();
        qw.eq("course_id", courseId);
        return eduVideoService.list(qw);
    }
}
